<?php

namespace Home\Controller;

use Common\Controller\HomebaseController;

class NewsController extends HomebaseController
{
    public function index()
    {
        $parentCid = 6;
        $currentCategory = get_one_category($parentCid);
        $categoryList = get_category($parentCid, '');
        $parentCategory = get_one_category($parentCid);
        $childList = get_category_childs($parentCid);
        $cid = I('get.cid', 0, 'intval');
        if ($cid > 0) {
            $whereArr['cid'] = $cid;
            $currentCategory = get_one_category($cid);
        }else{
            $whereArr['cid'] = array('in', implode(',',$childList));
        }
        //print_r($whereArr);
        $k = I('get.k');
        if($k){
            $whereArr['title'] = array('like', '%'. $k . '%');
        }
        $whereArr['status'] = 1;
        $model = M("Article");
        $pageSize = 7;
        $count = $model
            ->where($whereArr)
            ->count();
        //echo $model->getLastSql();
        $page = $this->page($count, $pageSize);
        $start = $page->firstRow;
        $list = $model
            ->where($whereArr)
            ->limit($start, $page->listRows)
            ->order("displayorder DESC,created_time DESC")
            ->select();
        //echo $model->getLastSql();
        $this->assign("total", $page->getTotalPages());
        $this->assign("p", isset($_GET['p']) ? intval($_GET['p']) : 1);
        $page->SetPager('newPage', '<div class="fs0 text-center fen">{first}{prev}{list}{next}{last}</div>');
        $this->assign("page", str_replace('index.php?g=Home&m=News&a=index&', 'news.html?', $page->show('newPage')));
        $this->assign("list", $list);
        $whereArr['cid'] = array('in', implode(',',$childList));
        $hotNews = get_where_list('article', $whereArr, 4, 'views DESC,');
        //print_r($hotNews);
        $this->assign("hotNews", $hotNews);
        $whereArr['recommended'] = 1;
        $recommendNews = get_where_list('article', $whereArr, 2, 'views DESC,');
        //print_r($recommendNews);
        $this->assign("recommendNews", $recommendNews);
        if (!empty($currentCategory['seo_title'])) {
            $seoTitle = $currentCategory['seo_title'] . ' - ';
        } else {
            $seoTitle = $currentCategory['name'] . ' - ';
        }
        $siteOptions = get_site_options();
        $this->assign('site_seo_title', $seoTitle . $siteOptions['site_name']);
        if (!empty($currentCategory['seo_keywords'])) {
            $this->assign('site_seo_keywords', $currentCategory['seo_keywords']);
        }
        if (!empty($currentCategory['seo_description'])) {
            $this->assign('site_seo_description', $currentCategory['seo_description']);
        }
        $this->assign('parentCategory', $parentCategory);
        $this->assign('currentCategory', $currentCategory);
        $this->assign('categoryList', $categoryList);
        $this->assign('cid', $cid);
        $this->display();
    }

    public function detail()
    {
        $model = M("Article");
        $id = I('get.id', 0, 'intval');
        $whereArr = array(
            'status' => 1,
        );
        if ($id > 0) {
            $whereArr['id'] = $id;
        }
        $model->save(array("id" => $id, "views" => array("exp", "views+1")));

        $post = $model
            ->where($whereArr)
            ->find();

        $next = $model
            ->where(array("id" => array('gt', $id), 'status' => 1, 'cid' => $post['cid']))
            ->order("created_time asc")
            ->find();

        $prev = $model
            ->where(array("id" => array('lt', $id), 'status' => 1, 'cid' => $post['cid']))
            ->order("created_time desc")
            ->find();

        $this->assign("next", $next);
        $this->assign("prev", $prev);

        $smeta = json_decode($post['smeta'], true);
        $this->assign('post', $post);
        $this->assign('cid', $post['cid']);
        $this->assign("smeta", $smeta);
        $siteOptions = get_site_options();
        $this->assign('site_seo_title', $post['title'] . ' - ' . $siteOptions['site_name']);
        if (!empty($post['keywords'])) {
            $this->assign('site_seo_keywords', $post['keywords']);
        }
        if (!empty($post['excerpt'])) {
            $this->assign('site_seo_description', $post['description']);
        }
        $oneCategory = get_one_category(1);
        $currentCategory = get_one_category($post['cid']);
        $this->assign('oneCategory', $oneCategory);
        $this->assign('currentCategory', $currentCategory);
        $this->display('detail');
    }
}